package com.usdt.system.service.impl.cp;

import java.math.BigDecimal;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.WeekFields;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usdt.common.core.domain.entity.SysDictData;
import com.usdt.common.exception.ServiceException;
import com.usdt.common.utils.bean.BeanUtil;
import com.usdt.system.cp.utils.UserAndDeptUtils;
import com.usdt.system.domain.cp.ActivityCustomerEntity;
import com.usdt.system.domain.cp.ActivityEntity;
import com.usdt.system.domain.cp.ActivityImgEntity;
import com.usdt.system.domain.cp.ActivityTempEntity;
import com.usdt.system.domain.req.ActivityImgReqVO;
import com.usdt.system.domain.req.ActivityListInsertReqVO;
import com.usdt.system.domain.req.ActivityListReqVO;
import com.usdt.system.domain.req.ActivityListV2ReqVO;
import com.usdt.system.domain.req.ActivityReqVO;
import com.usdt.system.domain.req.ActivitySignUpReqVO;
import com.usdt.system.domain.req.ActivitySummaryReqVO;
import com.usdt.system.domain.req.ActivityTempReqVO;
import com.usdt.system.domain.req.ActivityUpdateStatusReqVO;
import com.usdt.system.domain.req.ActivitylistExportReqVO;
import com.usdt.system.domain.resp.ActivityCalendarV1Resp;
import com.usdt.system.domain.resp.ActivityCalendarV2Resp;
import com.usdt.system.domain.resp.ActivityCalendarV3Resp;
import com.usdt.system.domain.resp.ActivityCustomerRespVO;
import com.usdt.system.domain.resp.ActivityRespVO;
import com.usdt.system.dto.cp.ActivityCalendarDTO;
import com.usdt.system.dto.cp.ActivityCustomerExcelDTO;
import com.usdt.system.dto.cp.ActivityDetailExcelDTO;
import com.usdt.system.dto.cp.ActivityListExcelDTO;
import com.usdt.system.dto.cp.ActivityPointsExcelDTO;
import com.usdt.system.dto.cp.WeeklyScheduleDTO;
import com.usdt.system.mapper.cp.ActivityMapper;
import com.usdt.system.service.ISysDictTypeService;
import com.usdt.system.service.cp.ActivityCustomerService;
import com.usdt.system.service.cp.ActivityImgService;
import com.usdt.system.service.cp.ActivityService;
import com.usdt.system.service.cp.ActivityTempService;

@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, ActivityEntity> 
									implements ActivityService{
	
	@Value("${sys.properties.ky.main_dept_id}")
	public String mainDeptId;
	
	@Autowired
	private ActivityTempService activityTempService;
	
	@Autowired
	private ActivityImgService activityImgService;
	
	@Autowired
	private ActivityCustomerService activityCustomerService;
	
    @Autowired
    private ISysDictTypeService dictTypeService;
	

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Integer insert(ActivityReqVO vo) {
		ActivityEntity activityEntity = BeanUtil.toBean(vo, ActivityEntity.class);
		if(!CollectionUtils.isEmpty(vo.getActivityProcessDescription())) {
			activityEntity.setActivityProcessDescriptionJson(JSON.toJSONString(vo.getActivityProcessDescription()));
		}
		Map<String, String> currentUserIdAndDeptId = UserAndDeptUtils.getCurrentUserIdAndDeptId();
		activityEntity.setDeptId(currentUserIdAndDeptId.get("currentDeptId"));
		activityEntity.setCreateBy(currentUserIdAndDeptId.get("currentUserId"));
		activityEntity.setUpdateBy(currentUserIdAndDeptId.get("currentUserId"));
		activityEntity.setActivityDurationMin(this.getActivityDurationMin(activityEntity.getActivityDuration(),activityEntity.getActivityDurationUnit()));
		
		Integer activityTempId = vo.getActivityTempId();
		//草稿  直接保存
		//根据模板新增 直接保存
		//新增  是否同步模板库
		if(null==vo.getActivityTempId() && vo.getIsSyn() && !vo.getIsDraft()) {
			ActivityTempReqVO bean = BeanUtil.toBean(vo, ActivityTempReqVO.class);
			bean.setId(null);
			activityTempId = activityTempService.insert(bean);
		}
		activityEntity.setActivityTempId(activityTempId);
		baseMapper.insert(activityEntity);
		return activityEntity.getId();
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void insertList(ActivityListReqVO vo) {
		List<ActivityListInsertReqVO> list = vo.getList();
		if(!CollectionUtils.isEmpty(list)) {
			for (ActivityListInsertReqVO req : list) {
				Integer id = req.getId();
				Integer activityTempId = req.getActivityTempId();
				ActivityReqVO bean = null;
				if(null!=id) {
					//根据id先去草稿状态查   是否同步模板库
					ActivityEntity selectOneByIdAndQuery = selectOneByIdAndQuery(id,true);
					if(null!=selectOneByIdAndQuery) {//草稿
						bean = BeanUtil.toBean(selectOneByIdAndQuery, ActivityReqVO.class);
						bean.setId(null);
						baseMapper.deleteById(selectOneByIdAndQuery.getId());
						bean.setIsDraft(false);
					}else {//复制活动
						ActivityEntity selectOne = selectOneByIdAndQuery(id,false);
						bean = BeanUtil.toBean(selectOne, ActivityReqVO.class);
						bean.setId(null);
						bean.setIsSyn(false);
						bean.setIsDraft(false);
					}
				}
				if(null!=activityTempId) {
					//根据模板id查  复制模板
					ActivityTempEntity selectById = activityTempService.selectById(activityTempId);
					bean = BeanUtil.toBean(selectById, ActivityReqVO.class);
					bean.setId(null);
					bean.setActivityTempId(selectById.getId());
				}
				if(null!=bean) {
					bean.setName(req.getName());
					bean.setActivityAddressTempId(req.getActivityAddressTempId());
					bean.setActivityAddress(req.getActivityAddress());
					bean.setActivityPeopleLimited(req.getActivityPeopleLimited());
					bean.setActivityTime(req.getActivityTime());
					insert(bean);
				}
			}
		}
	}
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void insertListV2(ActivityListV2ReqVO vo) {
		List<ActivityReqVO> list = vo.getList();
		if(!CollectionUtils.isEmpty(list)) {
			for (ActivityReqVO req : list) {
				//新增
				if(null==req.getId() && null==req.getActivityTempId()) {
					req.setIsDraft(false);
					insert(req);
				}
				//复制
				if(null!=req.getId() && null==req.getActivityTempId()) {
					req.setId(null);
					req.setIsSyn(false);
					req.setIsDraft(false);
					insert(req);
				}
				//模板创建
				if(null==req.getId() && null!=req.getActivityTempId()) {
					req.setId(null);
					req.setIsSyn(false);
					req.setIsDraft(false);
					insert(req);
				}
			}
		}
	}
	
	private ActivityEntity selectOneByIdAndQuery(Integer id, Boolean isDraft) {
		QueryWrapper<ActivityEntity> query = new QueryWrapper<ActivityEntity>();
		query.eq("id", id);
		if(null!=isDraft) {
			query.eq("is_draft", isDraft);
		}
		ActivityEntity selectOne = baseMapper.selectOne(query);
		return selectOne;
	}

	@Override
	public ActivityRespVO detail(Integer id) {
		ActivityEntity activityEntity = baseMapper.selectById(id);
		ActivityRespVO result = BeanUtil.toBean(activityEntity,ActivityRespVO.class);
		//sysDictDataService.selectDictLabel(DOCT_TYPE, activityTempEntity.getDeptId());
		//图片
		List<ActivityImgEntity> imgList = activityImgService.list(id,(byte)1);
		result.setImgList(imgList.stream().map(e->e.getUrl()).collect(Collectors.toList()));
		//视频
		List<ActivityImgEntity> vdList = activityImgService.list(id,(byte)2);
		result.setVideoList(vdList.stream().map(e->e.getUrl()).collect(Collectors.toList()));
		//人员
		List<ActivityCustomerEntity> customerList = activityCustomerService.list(id);
		result.setActivityCustomerList(BeanUtil.toBeans(customerList, ActivityCustomerRespVO.class));
		
		return result;
	}
	
	@Override
	public Integer count(String startDate,String endDate) {
		String currentDeptId = UserAndDeptUtils.getCurrentDeptId();
		if(mainDeptId.equals(currentDeptId)) {
			currentDeptId=null;
		}
		
		ActivityCalendarV1Resp result = new ActivityCalendarV1Resp();
		QueryWrapper<ActivityEntity> query = new QueryWrapper<ActivityEntity>();
		query.eq("status", (byte)1)
			.eq("is_draft", false)
			.ge("activity_time", startDate+" 00:00:00")
			.le("activity_time", endDate+" 23:59:59");
		if(StringUtils.isNotBlank(currentDeptId)) {
			query.eq("dept_id", currentDeptId);
		}
		List<ActivityEntity> selectList = baseMapper.selectList(query);
		if(!CollectionUtils.isEmpty(selectList)) {
			return selectList.size();
		}
		return 0;
	}
	@Override
	public IPage<ActivityRespVO> list(Integer activityCategory, Integer activityType, String keyWord, Byte activityStatus, String startDate,
			String endDate, Integer pageNum, Integer pageSize) {
		String currentDeptId = UserAndDeptUtils.getCurrentDeptId();
		if(mainDeptId.equals(currentDeptId)) {
			currentDeptId=null;
		}
		
		IPage<ActivityRespVO> page =baseMapper.selectPageList1(new Page<>(pageNum, pageSize),activityCategory,
				activityType,keyWord,activityStatus,startDate,endDate,currentDeptId);
		
		return page.convert(e->{
			Integer activityId= e.getId();
			//图片
			List<ActivityImgEntity> imgList = activityImgService.list(activityId,(byte)1);
			e.setImgList(imgList.stream().map(p->p.getUrl()).collect(Collectors.toList()));
			//视频
			List<ActivityImgEntity> vdList = activityImgService.list(activityId,(byte)2);
			e.setVideoList(vdList.stream().map(p->p.getUrl()).collect(Collectors.toList()));
			//人员
			List<ActivityCustomerEntity> customerList = activityCustomerService.list(activityId);
			e.setActivityCustomerList(BeanUtil.toBeans(customerList, ActivityCustomerRespVO.class));
			return e;
		});
	}
	
	@Override
	public ActivityCalendarV1Resp calendarList(String startDate,String endDate) {
		String currentDeptId = UserAndDeptUtils.getCurrentDeptId();
		if(mainDeptId.equals(currentDeptId)) {
			currentDeptId=null;
		}
		
		ActivityCalendarV1Resp result = new ActivityCalendarV1Resp();
		LocalDate start = stringToLocalDate(startDate, ft);
		LocalDate end = stringToLocalDate(endDate, ft);
		String weekOfMonth = numberToString(getWeekOfMonth(start));
		result.setDate(localDateToString(start,ftt)+"(第"+weekOfMonth+"周)");

		QueryWrapper<ActivityEntity> query = new QueryWrapper<ActivityEntity>();
		query.eq("status", (byte)1)
			.eq("is_draft", false)
			.ge("activity_time", startDate+" 00:00:00")
			.le("activity_time", endDate+" 23:59:59")
			.orderByAsc("activity_time");
		if(StringUtils.isNotBlank(currentDeptId)) {
			query.eq("dept_id", currentDeptId);
		}
		List<ActivityEntity> selectList = baseMapper.selectList(query);
		if(CollectionUtils.isEmpty(selectList)) {
			selectList = new ArrayList<ActivityEntity>();
		}
		List<ActivityCalendarDTO> beans = BeanUtil.toBeans(selectList, ActivityCalendarDTO.class);
		Map<String, List<ActivityCalendarDTO>> map = beans.stream().collect(Collectors.groupingBy(ActivityCalendarDTO::getLocalDateStr));
		
		List<ActivityCalendarV2Resp> v2List = new ArrayList<ActivityCalendarV2Resp>();
		result.setV2List(v2List);
		
		//查询维度
		List<SysDictData> categoryData = dictTypeService.selectDictDataByType("activity_category");
		//查询类别
		List<SysDictData> typeData = dictTypeService.selectDictDataByType("activity_type");
		
		//前4天  后3天 活动数量的最大值
		List<Integer> v1 = new ArrayList<Integer>();
		List<Integer> v2 = new ArrayList<Integer>();
		for (int i = 0; i < 7; i++) {
			LocalDate temp = start.plusDays(i);
			String tempStr = localDateToString(temp,ft);
			//实体v2
			ActivityCalendarV2Resp v2Resp = new ActivityCalendarV2Resp();
			v2Resp.setMonth(localDateToString(temp,fttt)+"星期"+numberToString(i+1));
			v2Resp.setDate(tempStr);
			v2List.add(v2Resp);
			
			List<ActivityCalendarDTO> list = map.get(tempStr);
			List<ActivityCalendarV3Resp> v3List = new ArrayList<ActivityCalendarV3Resp>();
			v2Resp.setV3List(v3List);
			if(!CollectionUtils.isEmpty(list)) {
				for (ActivityCalendarDTO dto : list) {
					ActivityRespVO selectOneById2 = baseMapper.selectOneById2(dto.getId());
					ActivityCalendarV3Resp v3 = BeanUtil.toBean(dto, ActivityCalendarV3Resp.class);
					
					v3.setActivityStatus(selectOneById2.getActivityStatus());
					v3.setTime(dto.getTime());
					v3.setName(dto.getName());
					v3.setActivityAddress(dto.getActivityAddress());
					v3.setActivtyPoints(dto.getActivtyPoints());
					v3.setId(dto.getId());
					v3.setActivityPeopleLimited(dto.getActivityPeopleLimited());
					v3.setActivityCategory(dto.getActivityCategory());
					v3.setActivityType(dto.getActivityType());
					if(null!=dto.getActivityCategory()) {
						SysDictData category = categoryData.stream().filter(e->(e.getDictValue()).equals(dto.getActivityCategory()+"")).findAny().orElse(null);
						v3.setActivityCategoryStr(category.getDictLabel());
					}
					if(null!=dto.getActivityType()) {
						SysDictData type = typeData.stream().filter(e->(e.getDictValue()).equals(dto.getActivityType()+"")).findAny().orElse(null);
						v3.setActivityTypeStr(type.getDictLabel());
					}
					v3List.add(v3);
				}
			}
			if(i<4) {
				v1.add(v3List.size());
			}
			if(i>=4) {
				v2.add(v3List.size());
			}
		}
		
		Integer v1Max = Collections.max(v1);
		Integer v2Max = Collections.max(v2);
		List<ActivityCalendarV2Resp> v2List2 = result.getV2List();
		for (int i = 0; i < v2List2.size(); i++) {
			List<ActivityCalendarV3Resp> v3List = v2List2.get(i).getV3List();
			if(i<4) {
				if(v3List.size()<v1Max) {
					int p = v1Max - v3List.size();
					for (int j = 0; j < p; j++) {
						ActivityCalendarV3Resp re = new ActivityCalendarV3Resp();
						v3List.add(re);
					}
				}
			}
			if(i>=4) {
				if(v3List.size()<v2Max) {
					int p = v2Max - v3List.size();
					for (int j = 0; j < p; j++) {
						ActivityCalendarV3Resp re = new ActivityCalendarV3Resp();
						v3List.add(re);
					}
				}
			}
			v2List2.get(i).setV3List(v3List);
		}
		result.setV2List(v2List2);
		return result;
	}
	
	//数字转为为汉字
	private String numberToString(int i) {
		
		return numbers.get(i);
	}
	
	//日期转化为string
	private LocalDate stringToLocalDate(String dateStr,DateTimeFormatter df) {
		
		return LocalDate.parse(dateStr, df);
	}
	private String localDateToString(LocalDate date,DateTimeFormatter df) {
		
		return date.format(df);
	}
	
	//string转化为日期
	
	//日期是当前月的第几周
	private int getWeekOfMonth(LocalDate date) {
		// 获取ISO标准的周字段（周一作为一周的开始）
        //WeekFields weekFields = WeekFields.of(Locale.getDefault());
		WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY, 1);
        //WeekFields weekFields = WeekFields.of(DayOfWeek.SUNDAY, 1); // 1 表示星期天作为一周的第一天
        int weekOfMonth = date.get(weekFields.weekOfMonth());
		return weekOfMonth;
	}

	private static final List<String> numbers =Arrays.asList("零", "一", "二", "三", "四", "五", "六", "日", "八", "九");
	private DateTimeFormatter ft = DateTimeFormatter.ofPattern("yyyy-MM-dd");
	private DateTimeFormatter ftt = DateTimeFormatter.ofPattern("yyyy年M月");
	private DateTimeFormatter fttt = DateTimeFormatter.ofPattern("M月d日");
	 
	public static void main(String[] args) {
		LocalDate date = LocalDate.of(2025, 6, 30); // 例如，2023年3月15日
		
		// 获取ISO标准的周字段（周一作为一周的开始）
        //WeekFields weekFields = WeekFields.of(Locale.getDefault());
		WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY, 1);
        //WeekFields weekFields = WeekFields.of(DayOfWeek.SUNDAY, 1); // 1 表示星期天作为一周的第一天
        int weekOfMonth = date.get(weekFields.weekOfMonth());
        System.out.println(weekOfMonth);
        
	}
	 
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Integer update(ActivityReqVO vo) {
		ActivityRespVO selectOne = baseMapper.selectOneById2(vo.getId());
		if(3!=selectOne.getActivityStatus()) {
			throw new ServiceException("只有未开始的活动才能修改，其他状态不能修改", -1);
		}
		//总结
		ActivityEntity activityEntity = BeanUtil.toBean(vo, ActivityEntity.class);
		if(!CollectionUtils.isEmpty(vo.getActivityProcessDescription())) {
			activityEntity.setActivityProcessDescriptionJson(JSON.toJSONString(vo.getActivityProcessDescription()));
		}
		if(null!=activityEntity.getActivityDuration() && null!=activityEntity.getActivityDurationUnit()){
			activityEntity.setActivityDurationMin(this.getActivityDurationMin(activityEntity.getActivityDuration(),activityEntity.getActivityDurationUnit()));
		}
		Map<String, String> currentUserIdAndDeptId = UserAndDeptUtils.getCurrentUserIdAndDeptId();
		activityEntity.setUpdateBy(currentUserIdAndDeptId.get("currentUserId"));
		baseMapper.updateById(activityEntity);
		return vo.getId();
	}
	
	@Override
	public Integer updateStatus(ActivityUpdateStatusReqVO vo) {
		ActivityEntity selectById = baseMapper.selectById(vo.getId());
		
		ActivityEntity activityEntity = new ActivityEntity();
		activityEntity.setId(vo.getId());
		activityEntity.setStatus(vo.getStatus());
		activityEntity.setCancelReason(vo.getCancelReason());
		activityEntity.setActivtyPoints(selectById.getActivtyPoints());
		Map<String, String> currentUserIdAndDeptId = UserAndDeptUtils.getCurrentUserIdAndDeptId();
		activityEntity.setUpdateBy(currentUserIdAndDeptId.get("currentUserId"));
		baseMapper.updateById(activityEntity);
		return vo.getId();
	}
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Integer signUp(ActivitySignUpReqVO vo) {
		//先删除
		activityCustomerService.del(vo.getId());
		
		//报名
		List<ActivityCustomerRespVO> activityCustomerList = vo.getActivityCustomerList();
		if(!CollectionUtils.isEmpty(activityCustomerList)) {
			List<ActivityCustomerEntity> collect = activityCustomerList.stream().map(e->{
				ActivityCustomerEntity entity = new ActivityCustomerEntity();
				entity.setActivityId(vo.getId());
				entity.setCustomerId(e.getCustomerId());
				entity.setCustomerName(e.getCustomerName());
				entity.setRoomName(e.getRoomName());
				return entity;
			}).collect(Collectors.toList());
			activityCustomerService.insertList(collect);
			return vo.getId();
		}
		return vo.getId();
	}
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Integer img(ActivityImgReqVO vo) {
		List<String> imgList = vo.getList();
		//先删除
		activityImgService.del(vo.getId(),vo.getType());
		
		if(!CollectionUtils.isEmpty(imgList)) {
			List<ActivityImgEntity> collect = imgList.stream().map(e->{
				ActivityImgEntity entity = new ActivityImgEntity();
				entity.setActivityId(vo.getId());
				entity.setType(vo.getType());
				entity.setUrl(e);
				return entity;
			}).collect(Collectors.toList());
			activityImgService.insertList(collect);
			return vo.getId();
		}
		return vo.getId();
	}
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Integer video(ActivityImgReqVO vo) {
		List<String> videoList = vo.getList();
		//先删除
		activityImgService.del(vo.getId(),vo.getType());
		
		if(!CollectionUtils.isEmpty(videoList)) {
			List<ActivityImgEntity> collect = videoList.stream().map(e->{
				ActivityImgEntity entity = new ActivityImgEntity();
				entity.setActivityId(vo.getId());
				entity.setType((byte)2);
				entity.setUrl(e);
				return entity;
			}).collect(Collectors.toList());
			activityImgService.insertList(collect);	
			return vo.getId();
		}
		return vo.getId();
	}
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Integer summary(ActivitySummaryReqVO vo) {
		ActivityEntity selectById = baseMapper.selectById(vo.getId());
		
		ActivityEntity activityEntity = new ActivityEntity();
		activityEntity.setId(vo.getId());
		activityEntity.setActivtyPoints(selectById.getActivtyPoints());
		activityEntity.setActivityRealDuration(vo.getActivityRealDuration());
		activityEntity.setActivityRealDurationUnit(vo.getActivityRealDurationUnit());
		activityEntity.setActivityRealPeople(vo.getActivityRealPeople());
		activityEntity.setRemark(vo.getRemark());
		//总结-活动实际时长
		if(null!=activityEntity.getActivityRealDuration() && null!=activityEntity.getActivityRealDurationUnit()) {
			activityEntity.setActivityRealDurationMin(this.getActivityDurationMin(activityEntity.getActivityRealDuration(),activityEntity.getActivityRealDurationUnit()));
		}
		Map<String, String> currentUserIdAndDeptId = UserAndDeptUtils.getCurrentUserIdAndDeptId();
		activityEntity.setUpdateBy(currentUserIdAndDeptId.get("currentUserId"));
		baseMapper.updateById(activityEntity);
		
		//图片
		List<String> imgList = vo.getImgList();
		//先删除
		activityImgService.del(vo.getId(),(byte)1);
		if(!CollectionUtils.isEmpty(imgList)) {
			List<ActivityImgEntity> collect = imgList.stream().map(e->{
				ActivityImgEntity entity = new ActivityImgEntity();
				entity.setActivityId(vo.getId());
				entity.setType((byte)1);
				entity.setUrl(e);
				return entity;
			}).collect(Collectors.toList());
			activityImgService.insertList(collect);
		}
		
		//视频
		List<String> videoList = vo.getVideoList();
		activityImgService.del(vo.getId(),(byte)2);
		if(!CollectionUtils.isEmpty(videoList)) {
			List<ActivityImgEntity> collect = videoList.stream().map(e->{
				ActivityImgEntity entity = new ActivityImgEntity();
				entity.setActivityId(vo.getId());
				entity.setType((byte)2);
				entity.setUrl(e);
				return entity;
			}).collect(Collectors.toList());
			activityImgService.insertList(collect);
		}
		
		return vo.getId();
	}
	
	@Override
	public List<ActivityListExcelDTO> listExport(ActivitylistExportReqVO vo) {
		String currentDeptId = UserAndDeptUtils.getCurrentDeptId();
		if(mainDeptId.equals(currentDeptId)) {
			currentDeptId=null;
		}
		List<ActivityListExcelDTO> list = baseMapper.selectList1(vo.getActivityCategory(),
				vo.getActivityType(),
				vo.getKeyWord(),
				vo.getActivityStatus(),
				vo.getStartDate(),
				vo.getEndDate(),
				currentDeptId);
		
		if(CollectionUtils.isEmpty(list)) {
			return new ArrayList<ActivityListExcelDTO>();
		}
		for (ActivityListExcelDTO activityListExcelDTO : list) {
			//参与人员
			List<ActivityCustomerEntity> customerList = activityCustomerService.list(activityListExcelDTO.getId());
			if(!CollectionUtils.isEmpty(customerList)) {
				List<String> collect = customerList.stream().map(e->e.getCustomerName()).collect(Collectors.toList());
				activityListExcelDTO.setCustomers(String.join("\n", collect));
			}
		}
		
		return list;
	}
	
	@Override
	public WeeklyScheduleDTO calendarListExport(ActivitylistExportReqVO vo) {
		WeeklyScheduleDTO result =new WeeklyScheduleDTO();
		ActivityCalendarV1Resp calendarList = calendarList(vo.getStartDate(),vo.getEndDate());
		
		result.setDate(calendarList.getDate());
		result.setWeek1(this.getWeekData(calendarList.getV2List().get(0)));
		result.setWeek1(calendarList.getV2List().get(0));
		result.setWeek2(calendarList.getV2List().get(1));
		result.setWeek3(calendarList.getV2List().get(2));
		result.setWeek4(calendarList.getV2List().get(3));
		result.setWeek5(calendarList.getV2List().get(4));
		result.setWeek6(calendarList.getV2List().get(5));
		result.setWeek7(calendarList.getV2List().get(6));
		return result;
	}
	
	private ActivityCalendarV2Resp getWeekData(ActivityCalendarV2Resp activityCalendarV2Resp) {
		ActivityCalendarV2Resp resul = activityCalendarV2Resp;
		List<ActivityCalendarV3Resp> v3List = resul.getV3List();
		if(!CollectionUtils.isEmpty(v3List)) {
			for (ActivityCalendarV3Resp v3Resp : v3List) {
				
				
				
			}
		}
		resul.setV3List(v3List);
		return resul;
	}

	@Override
	public List<ActivityDetailExcelDTO> detailExport(ActivitylistExportReqVO vo) {
		Map<String, Object> map = baseMapper.selectOneById1(vo.getId());
		List<ActivityDetailExcelDTO> list = new ArrayList<ActivityDetailExcelDTO>();
		if(null!=map&&map.size()>0) {
			map.forEach((k,v)->{
				ActivityDetailExcelDTO dto = new ActivityDetailExcelDTO();
				dto.setName(k);
				dto.setValue(v+"");
				if("流程说明".equals(k)) {
					if (StringUtils.isNotBlank(v+"")) {
			    		List<String> array = JSON.parseArray(v+"", String.class);
			    		dto.setValue(String.join("\n", array));
					}
				}
				list.add(dto);
			});
		}
		return list;
	}

	@Override
	public List<ActivityCustomerExcelDTO> customerExport(ActivitylistExportReqVO vo) {
		
		List<ActivityCustomerEntity> selectList = activityCustomerService.selectList(vo.getId());
		return BeanUtil.toBeans(selectList, ActivityCustomerExcelDTO.class);
	}
	
	//活动时长转化为分钟
	private BigDecimal getActivityDurationMin(BigDecimal activityDuration, Integer activityDurationUnit) {
		if(null==activityDuration) {
			return activityDuration;
		}
		if(1==activityDurationUnit){//分钟
			return activityDuration;
		}
		if(2==activityDurationUnit){//小时
			return activityDuration.multiply(new BigDecimal("60"));
		}
		if(3==activityDurationUnit){//天
			return activityDuration.multiply(new BigDecimal("24").multiply(new BigDecimal("60")));
		}
		return activityDuration;
	}

	@Override
	public List<ActivityPointsExcelDTO> pointsExport(ActivitylistExportReqVO vo) {
		String currentDeptId = UserAndDeptUtils.getCurrentDeptId();
		if(mainDeptId.equals(currentDeptId)) {
			currentDeptId=null;
		}
		List<ActivityPointsExcelDTO> result = baseMapper.getPoints(currentDeptId,vo.getStartDate(),vo.getEndDate());
		return result;
	}

}
